﻿using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Data.Util;
using JNPF.Utils;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 数据备份
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.20
    /// </summary>
    internal class DbBackupService : DataFactory, DbBackupIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="requestParam">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<DbBackupEntity>> GetList(PageInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<DbBackupEntity>();
                var queryParam = input.queryJson.ToObject();
                if (!string.IsNullOrEmpty(input.keyword))
                {
                    queryWhere = queryWhere.And(m => m.FileName.Contains(input.keyword) || m.FilePath.Contains(input.keyword));
                }
                return await db.GetIQueryable<DbBackupEntity>().Where(queryWhere).OrderByDescending(t => t.CreatorTime).GetPageResultAsync(input);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<DbBackupEntity> GetInfo(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<DbBackupEntity>(m => m.Id == id);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(DbBackupEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(DbBackupEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }
        /// <summary>
        /// 备份
        /// </summary>
        /// <param name="disk">路径</param>
        public async Task DbBackup(string disk)
        {
            using (var db = this.Basic)
            {
                var dbName = db.dbContext.Database.GetDbConnection().Database;
                var strSql = string.Format("backup database {0} to disk ='{1}'", dbName, disk);
                await db.ExecuteSqlAsync(strSql);
            }
        }
        /// <summary>
        /// 还原
        /// </summary>
        /// <param name="disk">路径</param>
        public async Task DbRestore(string disk)
        {
            using (var db = this.Basic)
            {
                var dbName = db.dbContext.Database.GetDbConnection().Database;
                var strSql = string.Format("restore database {0} from disk='{1}'", dbName, disk);
                await db.ExecuteSqlAsync(strSql);
            }
        }
    }
}
